package org.javaforever.gatescore.reports;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontDropdown;
import org.javaforever.gatescore.core.FrontField;
import org.javaforever.gatescore.core.FrontMethod;
import org.javaforever.gatescore.core.FrontVar;
import org.javaforever.gatescore.core.FrontVerb;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.UtilsMethodGenerator;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.reports.verb.CompareSetup;
import org.javaforever.gatescore.reports.verb.ListActiveCompareOption;
import org.javaforever.gatescore.reports.verb.ListActiveCompareSumOption;
import org.javaforever.gatescore.reports.verb.Translate;
import org.javaforever.gatescore.utils.StringUtil;
import org.javaforever.gatescore.verb.ActivateAll;
import org.javaforever.gatescore.verb.ListActive;
import org.javaforever.gatescore.verb.SearchByFieldsByPage;
import org.javaforever.gatescore.verb.SoftDeleteAll;
import org.javaforever.gatescore.widgets.Layout;
import org.javaforever.gatescore.widgets.ToggleMenubar;
import org.javaforever.gatescore.widgets.ViewDialog;

public class EchartsCompareGridReportLayout extends Layout{
	protected FrontDomain planDomain;
	protected FrontDomain actionDomain;
	protected List<FrontField> planxAxisFields;
	protected FrontField planyName;
	protected List<FrontField> actionxAxisFields;
	protected FrontField actionyName;
	protected EchartsCompareDiagram diagram;
	protected DiagramDataControlGrid planGrid;
	protected DiagramDataControlGrid actionGrid;
	protected ViewDialog viewPlanDialog;
	protected ViewDialog viewActionDialog;
	protected ToggleMenubar tmenubar;
	protected ToggleMenubar tmenubar2;

	@Override
	public StatementList generateLayoutStatements() throws Exception{	
		List<Writeable> sList = new ArrayList<>();
		sList.add(new Statement(1000L,1,"<div>"));
		StatementList sl = diagram.generateWidgetStatements();
		sl.setIndent(2);
		sl.setSerial(2000L);
		
		StatementList tsl = tmenubar.generateWidgetStatements();
		tsl.setIndent(2);
		tsl.setSerial(3000L);
		
		StatementList sl2 = planGrid.generateWidgetStatements();
		sl2.setIndent(2);
		sl2.setSerial(4000L);
		
		StatementList sl3 = viewPlanDialog.generateWidgetStatements();
		sl3.setIndent(2);
		sl3.setSerial(5000L);	
		sList.add(sl);
		sList.add(tsl);
		sList.add(sl2);
		sList.add(sl3);
		
		sList.add(new Statement(6000L,2,"<el-button-group>"));
		sList.add(new Statement(7000L,3,"<el-button size=\"mini\" type=\"success\" icon=\"el-icon-search\" @click=\"showViewDialog2\" :disabled=\"this.selectList2.length==0 || this.selectList2.length > 1\">查看</el-button>"));
		sList.add(new Statement(8000L,3,"<el-button :disabled=\"this.selectList2.length==0\" size=\"mini\" type=\"primary\" icon=\"el-icon-star-off\" @click=\"activateAll"+this.actionDomain.getCapFirstPlural()+"\">批激活</el-button>"));
		sList.add(new Statement(9000L,3,"<el-button :disabled=\"this.selectList2.length==0\" size=\"mini\" type=\"primary\" icon=\"el-icon-delete\" @click=\"softDeleteAll"+this.actionDomain.getCapFirstPlural()+"\">批软删除</el-button>"));
		sList.add(new Statement(10000L,2,"</el-button-group>"));
		
		
		long serial = 11000L;
		sList.add(new Statement(serial,2,"<el-table :data=\"list2\" :header-cell-style=\"{'background-color': '#3b8fce','color':'white'}\" :row-style=\"selectedHighlight2\" v-loading=\"listLoading2\" ref=\"table2\" border fit highlight-current-row style=\"width: 100%\" @select-all=\"handleCheckedAll2\" @selection-change=\"handleChecked2\">"));
		sList.add(new Statement(serial+2000L,0,""));
		sList.add(new Statement(serial+3000L,3,"<el-table-column type=\"selection\" align=\"center\" width=\"40px\"/>"));
		sList.add(new Statement(serial+5000L,0,""));
		FrontField fid = this.actionDomain.getDomainId()==null?null: this.actionDomain.getDomainId();
		if (fid != null ) {
			sList.add(new Statement(serial+5200L,3,"<el-table-column width=\"80px\" align=\"center\" label=\""+fid.getText()+"\">"));
			sList.add(new Statement(serial+6000L,4,"<template slot-scope=\"scope\">"));
			sList.add(new Statement(serial+7000L,5,"<span>{{ scope.row."+fid.getFieldName()+" }}</span>"));
			sList.add(new Statement(serial+8000L,4,"</template>"));
			sList.add(new Statement(serial+9000L,3,"</el-table-column>"));
		}
		serial = serial+11000L;
		for (FrontField f:this.actionDomain.getAllFieldsWithoutId()) {
			if (!(f instanceof FrontDropdown)) {
			if (f.getFieldType()!=null&&f.getFieldType().getTypeName()!=null&&f.getFieldType().getTypeName().equalsIgnoreCase("image")) {
					sList.add(new Statement(serial,3,"<el-table-column width=\"160px\" align=\"center\" label=\""+f.getText()+"\" :show-overflow-tooltip=\"true\">"));
					sList.add(new Statement(serial+1000L,4,"<template slot-scope=\"scope\">"));
					sList.add(new Statement(serial+2000L,5,"<img v-if=\"!scope.row."+f.getLowerFirstFieldName()+"\"　:src=\"'../static/images/blank.jpg'\" height=\"50\"/>"));
					sList.add(new Statement(serial+3000L,5,"<img v-else :src=\"'data:image/png;base64,'+scope.row."+f.getLowerFirstFieldName()+"\" height=\"50\"/>"));
					sList.add(new Statement(serial+4000L,4,"</template>"));
					sList.add(new Statement(serial+5000L,3,"</el-table-column>"));
				}else {
					sList.add(new Statement(serial,3,"<el-table-column width=\"160px\" align=\"center\" label=\""+f.getText()+"\">"));
					sList.add(new Statement(serial+1000L,4,"<template slot-scope=\"scope\">"));
					sList.add(new Statement(serial+2000L,5,"<span>{{ scope.row."+f.getFieldName()+" }}</span>"));
					sList.add(new Statement(serial+3000L,4,"</template>"));
					sList.add(new Statement(serial+4000L,3,"</el-table-column>"));
				}
				serial = serial + 6000L;
			}else {
				FrontDropdown dp = (FrontDropdown)f;
				sList.add(new Statement(serial,3,"<el-table-column width=\"160px\" align=\"center\" label=\""+dp.getText()+"\">"));
				sList.add(new Statement(serial+1000L,4,"<template slot-scope=\"scope\">"));
				sList.add(new Statement(serial+2000L,5,"{{ scope.row."+dp.getAliasName()+" | "+dp.getTarget().getLowerFirstDomainName()+"Filter }}"));
				sList.add(new Statement(serial+3000L,4,"</template>"));
				sList.add(new Statement(serial+4000L,3,"</el-table-column>"));
				serial = serial + 6000L;
			}			
		}
		sList.add(new Statement(serial,2,"</el-table>"));
		sList.add(new Statement(serial+2000L,2,"<pagination v-show=\"total2>0\" :total=\"total2\" :page.sync=\"listQuery2.page\" :limit.sync=\"listQuery2.rows\" @pagination=\"search"+this.actionDomain.getCapFirstPlural()+"ByFieldsByPage\" />"));
		sList.add(new Statement(serial+8000L,2,"<el-dialog"));
		sList.add(new Statement(serial+9000L,3,":visible.sync=\"viewDialogVisible2\""));
		sList.add(new Statement(serial+10000L,3,":before-close=\"handleClose2\""));
		if (this.actionDomain.getLanguage().equalsIgnoreCase("english")) {
			sList.add(new Statement(serial+11000L,3,"title=\"View\""));
		}else {
			sList.add(new Statement(serial+11000L,3,"title=\"查看\""));
		}
		sList.add(new Statement(serial+12000L,3,"width=\"850px\">"));
		sList.add(new Statement(serial+13000L,3,"<el-form :model=\"form\" label-position=\"right\" label-width=\"120px\">"));
		sList.add(new Statement(serial+14000L,4,"<el-input v-model=\"form."+this.actionDomain.getDomainId().getLowerFirstFieldName()+"\" type=\"hidden\"/>"));
		sList.add(new Statement(serial+15000L,4,"<el-row>"));
		serial = serial + 16000L;
		for (FrontField f:this.actionDomain.getAllFieldsWithoutId()) {
			if (!(f instanceof FrontDropdown)) {
				if (f.getFieldType().getTypeName().equalsIgnoreCase("image")) {
					sList.add(new Statement(serial+1000L,5,"<el-col :span=\"12\">"));
					sList.add(new Statement(serial+2000L,6,"<el-form-item label=\""+f.getText()+"\">"));
					sList.add(new Statement(serial+3000L,7,"<img v-if=\"!form."+f.getLowerFirstFieldName()+"\" :src=\"'../static/images/blank.jpg'\" width=\"300\">"));
					sList.add(new Statement(serial+4000L,7,"<img v-else :src=\"'data:image/png;base64,'+form."+f.getLowerFirstFieldName()+"\" width=\"300\">"));
					sList.add(new Statement(serial+5000L,6,"</el-form-item>"));
					sList.add(new Statement(serial+6000L,5,"</el-col>"));
				}else if (f.getFieldType().getTypeName().equalsIgnoreCase("boolean")){
					sList.add(new Statement(serial,5,"<el-col :span=\"12\">"));
					sList.add(new Statement(serial+1000L,6,"<el-form-item label=\""+f.getText()+"\">"));
					sList.add(new Statement(serial+2000L,7,"<el-input v-model=\"''+form."+f.getFieldName()+"\" style=\"width: 240px;\"/>"));
					sList.add(new Statement(serial+3000L,6,"</el-form-item>"));
					sList.add(new Statement(serial+4000L,5,"</el-col>"));
				}else {
					sList.add(new Statement(serial,5,"<el-col :span=\"12\">"));
					sList.add(new Statement(serial+1000L,6,"<el-form-item label=\""+f.getText()+"\">"));
					sList.add(new Statement(serial+2000L,7,"<el-input v-model=\"form."+f.getFieldName()+"\" style=\"width: 240px;\"/>"));
					sList.add(new Statement(serial+3000L,6,"</el-form-item>"));
					sList.add(new Statement(serial+4000L,5,"</el-col>"));
				}
				serial = serial + 12000L;
			}else {
				FrontDropdown dp = (FrontDropdown)f;
				sList.add(new Statement(serial,5,"<el-col :span=\"12\">"));
				sList.add(new Statement(serial+1000L,6,"<el-form-item label=\""+dp.getText()+"\" prop=\""+dp.getAliasOrName()+"\">"));
				if (this.actionDomain.getLanguage().equalsIgnoreCase("english")) {
					sList.add(new Statement(serial+2000L,7,"<el-select v-model=\"form."+dp.getAliasName()+"\" value-key=\""+dp.getTarget().getDomainId().getFieldName()+"\" placeholder=\"Please choose \" style=\"width: 240px;\" @change=\"translate"+dp.getTarget().getCapFirstDomainName()+"\">"));
				}else {
					sList.add(new Statement(serial+2000L,7,"<el-select v-model=\"form."+dp.getAliasName()+"\" value-key=\""+dp.getTarget().getDomainId().getFieldName()+"\" placeholder=\"请选择\" style=\"width: 240px;\" @change=\"translate"+dp.getTarget().getCapFirstDomainName()+"\">"));
				}
				sList.add(new Statement(serial+3000L,8,"<el-option"));
				sList.add(new Statement(serial+4000L,9,"v-for=\"item in active"+dp.getTarget().getCapFirstPlural()+"\""));
				sList.add(new Statement(serial+5000L,9,":key=\"item."+dp.getAliasName()+"\""));
				sList.add(new Statement(serial+6000L,9,":label=\"item."+dp.getTarget().getDomainName().getFieldName()+"\""));
				sList.add(new Statement(serial+7000L,9,":value=\"item."+dp.getTarget().getDomainId().getFieldName()+"\"/>"));
				sList.add(new Statement(serial+9000L,7,"</el-select>"));
				sList.add(new Statement(serial+10000L,6,"</el-form-item>"));
				sList.add(new Statement(serial+11000L,5,"</el-col>"));
				serial = serial + 12000L;
			}
		}
		sList.add(new Statement(serial,4,"</el-row>"));
		sList.add(new Statement(serial+1000L,3,"</el-form>"));
		sList.add(new Statement(serial+2000L,3,"<span slot=\"footer\" class=\"dialog-footer\">"));
		if (this.actionDomain.getLanguage().equalsIgnoreCase("english")) {
			sList.add(new Statement(serial+3000L,4,"<el-button @click=\"viewDialogVisible2 = false\">Close</el-button>"));
		}else {
			sList.add(new Statement(serial+3000L,4,"<el-button @click=\"viewDialogVisible2 = false\">关  闭</el-button>"));
		}
		sList.add(new Statement(serial+5000L,3,"</span>"));
		sList.add(new Statement(serial+6000L,2,"</el-dialog>"));
		
		sList.add(new Statement(serial+7000L,1,"</div>"));
		
		StatementList result =  WriteableUtil.merge(sList);
		result.setSerial(this.serial);
		return result;
	}

	@Override
	public StatementList generateLayoutScriptStatements() throws Exception{
		List<Writeable> sList = new ArrayList<>();
		long serial = 0L;
		sList.add(new Statement(serial+1000L,0,"import {"));		
		StringBuilder sb = new StringBuilder("search"+this.planDomain.getCapFirstPlural()+"ByFieldsByPage, ");
		if (!(this.planDomain instanceof org.javaforever.gatescore.core.FrontEnum)) {
			sb.append("softDeleteAll"+this.planDomain.getCapFirstPlural()+", ");	
			sb.append("activateAll"+this.planDomain.getCapFirstPlural()+", ");
		}
		ListActive la = new ListActive(this.planDomain);
		sb.append(StringUtil.lowerFirst(la.getVerbName()));
		sList.add(new Statement(serial+2000L,1,sb.toString()));
		sList.add(new Statement(serial+3000L,0,"} from '@/api/"+this.planDomain.getPlural().toLowerCase()+"'"));
		
		serial += 4000L;
		sList.add(new Statement(serial+1000L,0,"import {"));		
		StringBuilder sb2 = new StringBuilder("search"+this.actionDomain.getCapFirstPlural()+"ByFieldsByPage, ");
		if (!(this.actionDomain instanceof org.javaforever.gatescore.core.FrontEnum)) {
			sb2.append("softDeleteAll"+this.actionDomain.getCapFirstPlural()+", ");	
			sb2.append("activateAll"+this.actionDomain.getCapFirstPlural()+", ");
		}
		ListActive la2 = new ListActive(this.actionDomain);
		sb2.append(StringUtil.lowerFirst(la2.getVerbName()));
		sList.add(new Statement(serial+2000L,1,sb2.toString()));
		sList.add(new Statement(serial+3000L,0,"} from '@/api/"+this.actionDomain.getPlural().toLowerCase()+"'"));
		
		serial += 4000L;
 		Set<FrontDomain> targets = new TreeSet<>();
		for (FrontField f:this.planDomain.getAllFields()) {
			if (f instanceof FrontDropdown) {
				targets.add(((FrontDropdown) f).getTarget());
			}
		}
		for (FrontField f:this.actionDomain.getAllFields()) {
			if (f instanceof FrontDropdown) {
				targets.add(((FrontDropdown) f).getTarget());
			}
		}
		for (FrontDomain fd:targets) {
			ListActive tla = new ListActive(fd);
			sList.add(new Statement(serial,0,"import {"));
			sList.add(new Statement(serial+1000L,1,StringUtil.lowerFirst(tla.getVerbName())));
			sList.add(new Statement(serial+2000L,0,"} from '@/api/"+fd.getPlural().toLowerCase()+"'"));
			serial += 3000L;
		}
		
		sList.add(new Statement(serial+1000L,0,"import { getToken } from '@/utils/auth'"));
		sList.add(new Statement(serial+2000L,0,"import pagevars from '@/pagevars'"));
		sList.add(new Statement(serial+3000L,0,"import Pagination from '@/components/Pagination' // Secondary package based on el-pagination"));
		sList.add(new Statement(serial+4000L,0,""));
		serial += 5000L;
		
		Set<FrontDomain> dpDomains = new TreeSet<FrontDomain>();
		FrontVar defaultForm = new FrontVar("defaultForm");
		FrontVar postForm = new FrontVar("postForm");
		FrontVar listQuery = new FrontVar("listQuery");
		FrontVar pagevars = new FrontVar("pagevars");
		FrontVar data = new FrontVar("data");
		
		FrontVar defaultForm2 = new FrontVar("defaultForm2");
		FrontVar postForm2 = new FrontVar("postForm2");
		FrontVar listQuery2 = new FrontVar("listQuery2");
		for (FrontField ff:this.planDomain.getAllFieldsWithoutId()) {
			if (ff instanceof FrontDropdown) {
				FrontDropdown dp = (FrontDropdown) ff;
				dpDomains.add(dp.getTarget());
			}
		}
		for (FrontField ff:this.actionDomain.getAllFieldsWithoutId()) {
			if (ff instanceof FrontDropdown) {
				FrontDropdown dp = (FrontDropdown) ff;
				dpDomains.add(dp.getTarget());
			}
		}
		serial += 5000L;
		
		StatementList sl = UtilsMethodGenerator.generateDefaultFormBlockStatements(this.planDomain, defaultForm);
		sl.setSerial(serial+1000L);
		sList.add(sl);
		
		sl = UtilsMethodGenerator.generateDefaultFormBlockStatements(this.actionDomain, defaultForm2);
		sl.setSerial(serial+1200L);
		sList.add(sl);
		
		sList.add(new Statement(serial+2000L,0,"export function isBlank(val) {"));
		sList.add(new Statement(serial+3000L,1,"if (val === undefined || val === null || val === '') return true"));
		sList.add(new Statement(serial+4000L,1,"else return false"));
		sList.add(new Statement(serial+5000L,0,"}"));
		sList.add(new Statement(serial+6000L,0,""));
		sList.add(new Statement(serial+7000L,0,"export default {"));
		sList.add(new Statement(serial+8000L,1,"name: 'Echart',"));
		sList.add(new Statement(serial+9000L,1,"inject: ['reload'],"));
		sList.add(new Statement(serial+10600L,1,"components: {"));
		sList.add(new Statement(serial+11000L,2,"Pagination"));
		sList.add(new Statement(serial+12000L,1,"},"));
		
		sList.add(new Statement(serial+16000L,1,"filters: {"));
		serial += 17000L;
		for (FrontDomain d:dpDomains) {
			sl = UtilsMethodGenerator.generateDomainFilterStatements(d, pagevars);
			sl.setSerial(serial);
			sl.setIndent(1);
			sList.add(sl);
			serial += 1000L;
		}
		sList.add(new Statement(serial,1,"},"));
		
		sList.add(new Statement(serial+1000L,1,"data() {"));
		sList.add(new Statement(serial+2000L,2,"return {"));
		sList.add(new Statement(serial+3000L,3,"option: {},"));
		if (this.planyName instanceof FrontDropdown) {
			FrontDomain target = ((FrontDropdown)this.planyName).getTarget();
			sList.add(new Statement(serial+4000L,3,"active"+StringUtil.capFirst(target.getPlural())+": [],"));
		}
		sList.add(new Statement(serial+5000L,3,"active"+StringUtil.capFirst(this.planDomain.getPlural())+": [],"));
		sList.add(new Statement(serial+6000L,3,"chartTypes: [{"));
		if (this.planDomain.getLanguage().equalsIgnoreCase("english")) {
			sList.add(new Statement(serial+7000L,4,"value: 'line',"));
			sList.add(new Statement(serial+8000L,4,"label: 'Line'"));
			sList.add(new Statement(serial+9000L,3,"}, {"));
			sList.add(new Statement(serial+10000L,4,"value: 'bar',"));
			sList.add(new Statement(serial+11000L,4,"label: 'Bar'"));
			sList.add(new Statement(serial+12000L,3,"}, {"));
			sList.add(new Statement(serial+13000L,4,"value: 'pie',"));
			sList.add(new Statement(serial+14000L,4,"label: 'Pie'"));
			sList.add(new Statement(serial+15000L,3,"}],"));
			sList.add(new Statement(serial+16000L,3,"chartType: 'line',"));
			sList.add(new Statement(serial+17000L,3,"dataTypes: [{"));
			sList.add(new Statement(serial+18000L,4,"value: 'original',"));
			sList.add(new Statement(serial+19000L,4,"label: 'Original'"));
			sList.add(new Statement(serial+20000L,3,"}, {"));
			sList.add(new Statement(serial+21000L,4,"value: 'sum',"));
			sList.add(new Statement(serial+22000L,4,"label: 'Sum'"));
			sList.add(new Statement(serial+23000L,3,"}],"));
			sList.add(new Statement(serial+24000L,3,"dataType: 'original',"));
		}else {
			sList.add(new Statement(serial+7000L,4,"value: 'line',"));
			sList.add(new Statement(serial+8000L,4,"label: '折线图'"));
			sList.add(new Statement(serial+9000L,3,"}, {"));
			sList.add(new Statement(serial+10000L,4,"value: 'bar',"));
			sList.add(new Statement(serial+11000L,4,"label: '柱状图'"));
			sList.add(new Statement(serial+12000L,3,"}, {"));
			sList.add(new Statement(serial+13000L,4,"value: 'pie',"));
			sList.add(new Statement(serial+14000L,4,"label: '饼图'"));
			sList.add(new Statement(serial+15000L,3,"}],"));
			sList.add(new Statement(serial+16000L,3,"chartType: 'line',"));
			sList.add(new Statement(serial+17000L,3,"dataTypes: [{"));
			sList.add(new Statement(serial+18000L,4,"value: 'original',"));
			sList.add(new Statement(serial+19000L,4,"label: '原始数据'"));
			sList.add(new Statement(serial+20000L,3,"}, {"));
			sList.add(new Statement(serial+21000L,4,"value: 'sum',"));
			sList.add(new Statement(serial+22000L,4,"label: '累加数据'"));
			sList.add(new Statement(serial+23000L,3,"}],"));
			sList.add(new Statement(serial+24000L,3,"dataType: 'original',"));
		}
		serial += 25000L;
		sList.add(new Statement(serial,3,postForm.getVarName()+": Object.assign({}, "+defaultForm.getVarName()+"),"));
		sList.add(new Statement(serial+1000L,3,"list: [],"));
		sList.add(new Statement(serial+2000L,3,"selectList: [],"));
		sList.add(new Statement(serial+3000L,3,"selectRows: [],"));
		sList.add(new Statement(serial+4000L,3,"total: 0,"));
		sList.add(new Statement(serial+5000L,3,"listLoading: true,"));
		sList.add(new Statement(serial+6000L,3,"cellValue: null,"));
		sList.add(new Statement(serial+7000L,3,"viewDialogVisible: false,"));
		
		serial += 8000L;
		sList.add(new Statement(serial,3,postForm2.getVarName()+": Object.assign({}, "+defaultForm2.getVarName()+"),"));
		sList.add(new Statement(serial+1000L,3,"list2: [],"));
		sList.add(new Statement(serial+2000L,3,"selectList2: [],"));
		sList.add(new Statement(serial+3000L,3,"selectRows2: [],"));
		sList.add(new Statement(serial+4000L,3,"total2: 0,"));
		sList.add(new Statement(serial+5000L,3,"listLoading2: true,"));
		sList.add(new Statement(serial+6000L,3,"cellValue2: null,"));
		sList.add(new Statement(serial+7000L,3,"viewDialogVisible2: false,"));
		
		serial += 8000L;
		for (FrontField f: planDomain.getFields()) {
			if (f.getFieldType()!=null&& f.getFieldType().getTypeName()!=null&&"image".equalsIgnoreCase(f.getFieldType().getTypeName())) {
				sList.add(new Statement(serial,3, f.getLowerFirstFieldName()+"ImageUrl: '',"));
				sList.add(new Statement(serial+1000L,3, f.getLowerFirstFieldName()+"File: '',"));
				serial += 2000L;
			}	
		}
		sList.add(new Statement(serial,3,"baseApi: process.env.BASE_API,"));
		sList.add(new Statement(serial+1000L,3,"token: getToken(),"));
		sList.add(new Statement(serial+2000L,3,"form: {},"));
		serial += 3000L;
		for (FrontDomain d: dpDomains) {
			sList.add(new Statement(serial,3,"active"+d.getCapFirstPlural()+": {},"));
			sList.add(new Statement(serial+1000L,3,"init"+d.getCapFirstDomainName()+"Store: "+pagevars.getVarName()+".init"+d.getCapFirstDomainName()+"Store,"));
			serial += 2000L;
		}
		
		sList.add(new Statement(serial,3,listQuery.getVarName()+": {"));
		sList.add(new Statement(serial+1000L,4,"page: 1,"));
		sList.add(new Statement(serial+2000L,4,"rows: 10"));
		sList.add(new Statement(serial+3000L,3,"},"));
		
		serial += 3000L;
		sList.add(new Statement(serial,3,listQuery2.getVarName()+": {"));
		sList.add(new Statement(serial+1000L,4,"page: 1,"));
		sList.add(new Statement(serial+2000L,4,"rows: 10"));
		sList.add(new Statement(serial+3000L,3,"}"));
		
		sList.add(new Statement(serial+4000L,2,"}"));
		sList.add(new Statement(serial+5000L,1,"},"));

		sl = UtilsMethodGenerator.generateCompareCreatedBlockStatements(this.planDomain,this.actionDomain, dpDomains);
		sl.setSerial(serial+5500L);
		sl.setIndent(1);
		sList.add(sl);
		
		sList.add(new Statement(serial+6000L,1,"mounted() {"));
		sList.add(new Statement(serial+7000L,2,"this.setup()"));
		sList.add(new Statement(serial+8000L,1,"},"));
		sList.add(new Statement(serial+9000L,1,"methods: {"));		
		
		StatementList sld = this.diagram.getSetup().generateControllerMethod().getMethodStatementList();
		sld.setSerial(serial+10000L);
		sld.setIndent(2);
		sList.add(sld);
		
		for (FrontDomain dpd: dpDomains) {
			ListActive tla = new ListActive(dpd);
			StatementList tsl = tla.generateControllerMethod().getMethodStatementList();
			tsl.setSerial(serial+12000L);
			sList.add(tsl);
		}
		StatementList osl = la.generateControllerMethod().getMethodStatementList();
		osl.setSerial(serial+13000L);
		sList.add(osl);
		
		StatementList osl2 = la2.generateControllerMethod().getMethodStatementList();
		osl2.setSerial(serial+13000L);
		sList.add(osl2);
	
		serial += 16000L;
		for (FrontDomain dpd: dpDomains) {
			Translate tsl = new Translate(dpd);
			StatementList tsls = tsl.generateControllerMethod().getMethodStatementList();
			tsls.setSerial(serial);
			tsls.setIndent(2);
			sList.add(tsls);
			serial += 2000L;
		}
		StatementList listoptionsl = this.diagram.getListOption().generateControllerMethod().getMethodStatementList();
		listoptionsl.setSerial(serial+1000L);
		listoptionsl.setIndent(2);
		sList.add(listoptionsl);
	
		StatementList listsumoptionsl = this.diagram.getListSumOption().generateControllerMethod().getMethodStatementList();
		listsumoptionsl.setSerial(serial+3000L);
		listsumoptionsl.setIndent(2);
		sList.add(listsumoptionsl);
		Set<FrontVerb> verbs = new TreeSet<>();
		ActivateAll ata = new ActivateAll(this.planDomain);
		SoftDeleteAll sda = new SoftDeleteAll(this.planDomain);
		SearchByFieldsByPage sfp = new SearchByFieldsByPage(this.planDomain);
		verbs.add(ata);
		verbs.add(sda);
		verbs.add(sfp);
		
		serial += 5000L;
		for (FrontVerb fv: verbs ) {
			FrontMethod m = fv.generateControllerMethod();
			if (m!= null) {
				m.setSerial(serial);
				sList.add(m.getMethodStatementList());
				serial += 1000L;
			}
		}
		
		sl = UtilsMethodGenerator.generateBuildIdsStatements(this.planDomain, data);
		sl.setSerial(serial+500L);
		sList.add(sl);	
		
		sl = UtilsMethodGenerator.generateBuildParamsStatements(this.planDomain, listQuery, postForm);
		sl.setSerial(serial+1000L);
		sList.add(sl);
		
		sl = UtilsMethodGenerator.generateHandleSizeChangeMethodStatements(listQuery);
		sl.setSerial(serial+2000L);
		sList.add(sl);
		
		sl = UtilsMethodGenerator.generateHandleCurrentChangeMethodStatements(listQuery);
		sl.setSerial(serial+3000L);
		sList.add(sl);
		
		sl = UtilsMethodGenerator.generateHandleCheckedMethodStatements();
		sl.setSerial(serial+4000L);
		sList.add(sl);
		
		sl = UtilsMethodGenerator.generateHandleCheckedAllMethodStatements(this.planDomain);
		sl.setSerial(serial+5000L);
		sList.add(sl);
		
		sl = UtilsMethodGenerator.generateSelectedHighlightMethodStatements();
		sl.setSerial(serial+6000L);
		sList.add(sl);
		
		sl = UtilsMethodGenerator.generateHandleCloseMethodStatements(this.planDomain.getLanguage());
		sl.setSerial(serial+7000L);
		sList.add(sl);
		
		sl = UtilsMethodGenerator.generateShowViewDialogMethodStatements();
		sl.setSerial(serial+8000L);
		sList.add(sl);	
		
		serial += 9000L;		
		sList.add(new Statement(serial+1000L, 2, "search" + this.actionDomain.getCapFirstPlural() + "ByFieldsByPage() {"));
		sList.add(new Statement(serial+2000L, 3, "this.listLoading2 = true"));
		sList.add(new Statement(serial+3000L, 3, "search" + this.actionDomain.getCapFirstPlural()
				+ "ByFieldsByPage(this.buildParams2(this.listQuery2, this.postForm2)).then("));
		sList.add(new Statement(serial+4000L, 4, "response => {"));
		sList.add(new Statement(serial+5000L, 5, "this.list2 = response.data.rows"));
		sList.add(new Statement(serial+6000L, 5, "this.total2 = response.data.total"));
		sList.add(new Statement(serial+7000L, 5, "this.listLoading2 = false"));
		sList.add(new Statement(serial+8000L, 4, "})"));
		sList.add(new Statement(serial+9000L, 2, "},"));
		
		serial += 10000L;
		sList.add(new Statement(serial+1000L, 2, "activateAll" + this.actionDomain.getCapFirstPlural() + "() {"));
		sList.add(new Statement(serial+2000L, 3, "var data = this.selectList2"));
		sList.add(new Statement(serial+3000L, 3, "if (data.length < 1) return"));
		sList.add(new Statement(serial+4000L, 3,
				"activateAll" + this.actionDomain.getCapFirstPlural() + "(this.buildIds2(data)).then(response => {"));
		sList.add(new Statement(serial+5000L, 4, "this.reload()"));
		sList.add(new Statement(serial+6000L, 3, "})"));
		sList.add(new Statement(serial+7000L, 2, "},"));
		serial += 8000L;
		sList.add(new Statement(serial+1000L, 2, "softDeleteAll" + this.actionDomain.getCapFirstPlural() + "() {"));
		sList.add(new Statement(serial+2000L, 3, "var data = this.selectList2"));
		sList.add(new Statement(serial+3000L, 3, "if (data.length < 1) return"));
		sList.add(new Statement(serial+4000L, 3,
				"softDeleteAll" + this.actionDomain.getCapFirstPlural() + "(this.buildIds2(data)).then(response => {"));
		sList.add(new Statement(serial+5000L, 4, "this.reload()"));
		sList.add(new Statement(serial+6000L, 3, "})"));
		sList.add(new Statement(serial+7000L, 2, "},"));
		serial += 8000L;
		sList.add(new Statement(serial+1000L,2,"buildIds2("+data.getVarName()+") {"));
		sList.add(new Statement(serial+2000L,3,"var retVal = ''"));
		sList.add(new Statement(serial+3000L,3,"for (var i = 0; i < "+data.getVarName()+".length; i++) {"));
		sList.add(new Statement(serial+4000L,4,"retVal += "+data.getVarName()+"[i]."+this.actionDomain.getDomainId().getFieldName()+" + ','"));
		sList.add(new Statement(serial+5000L,3,"}"));
		sList.add(new Statement(serial+6000L,3,"if (retVal.indexOf(',') >= 0) retVal = retVal.substring(0, retVal.length - 1)"));
		sList.add(new Statement(serial+7000L,3,"return retVal"));
		sList.add(new Statement(serial+8000L,2,"},"));
		serial += 9000L;
		sList.add(new Statement(serial+1000L,2,"buildParams2("+listQuery2.getVarName()+", "+postForm2.getVarName()+") {"));
		sList.add(new Statement(serial+2000L,3,"return {"));
		sList.add(new Statement(serial+3000L,4,"page: "+listQuery2.getVarName()+".page,"));
		sList.add(new Statement(serial+4000L,4,"rows: "+listQuery2.getVarName()+".rows,"));
		serial += 5000L;
		for (FrontField f:this.actionDomain.getAllFieldsWithoutId()) {
			sList.add(new Statement(serial,4,f.getFieldName()+": "+postForm2.getVarName()+"."+f.getFieldName()+","));
			serial += 1000L;
		}
		sList.add(new Statement(serial,4,"token: getToken(),"));
		sList.add(new Statement(serial+1000L,3,"}"));
		sList.add(new Statement(serial+2000L,2,"},"));
		serial+=3000L;
		sList.add(new Statement(serial+1000L,2,"handleSizeChange2(val) {"));
		sList.add(new Statement(serial+2000L,3,"this."+listQuery2.getVarName()+".page = val"));
		sList.add(new Statement(serial+3000L,3,"this.getList2()"));
		sList.add(new Statement(serial+4000L,2,"},"));
		serial+=5000L;
		sList.add(new Statement(serial+1000L,2,"handleCurrentChange2(val) {"));
		sList.add(new Statement(serial+2000L,3,"this."+listQuery2.getVarName()+".rows = val"));
		sList.add(new Statement(serial+3000L,3,"this.getList2()"));
		sList.add(new Statement(serial+4000L,2,"},"));
		
		serial+=4000L;
		sList.add(new Statement(serial+1000L,2,"handleChecked2(data) {"));
		sList.add(new Statement(serial+2000L,3,"this.selectList2 = data"));
		sList.add(new Statement(serial+3000L,2,"},"));
		serial+=4000L;
		sList.add(new Statement(serial+1000L,2,"handleCheckedAll2(data) {"));
		sList.add(new Statement(serial+2000L,3,"this.selectList2 = data"));
		sList.add(new Statement(serial+3000L,2,"},"));
		serial+=4000L;
		sList.add(new Statement(serial+1000L,2,"selectedHighlight2({"));
		sList.add(new Statement(serial+2000L,3,"row,"));
		sList.add(new Statement(serial+3000L,3,"rowIndex"));
		sList.add(new Statement(serial+4000L,2,"}) {"));
		sList.add(new Statement(serial+5000L,3,"if (this.selectRows2.includes(rowIndex)) {"));
		sList.add(new Statement(serial+6000L,4,"return {"));
		sList.add(new Statement(serial+7000L,5,"\"background-color\": \"#a1c3c0\""));
		sList.add(new Statement(serial+8000L,4,"};"));
		sList.add(new Statement(serial+9000L,3,"} else {"));
		sList.add(new Statement(serial+10000L,4,"return {"));
		sList.add(new Statement(serial+11000L,5,"\"background-color\": \"#e8ebd0\""));
		sList.add(new Statement(serial+12000L,4,"};"));
		sList.add(new Statement(serial+13000L,3,"}"));
		sList.add(new Statement(serial+14000L,2,"},"));
		serial+=15000L;
		sList.add(new Statement(serial+1000L,2,"handleClose2(done) {"));
		if ("english".equalsIgnoreCase(language)) {
			sList.add(new Statement(serial+2000L,3,"this.$confirm('Confirm close?')"));
		}else{
			sList.add(new Statement(serial+2000L,3,"this.$confirm('确认关闭？')"));
		}
		sList.add(new Statement(serial+3000L,4,".then(_ => {"));
		sList.add(new Statement(serial+4000L,5,"done()"));
		sList.add(new Statement(serial+5000L,4,"})"));
		sList.add(new Statement(serial+6000L,4,".catch(_ => {})"));
		sList.add(new Statement(serial+7000L,2,"},"));
		serial+=8000L;
		sList.add(new Statement(serial+1000L,2,"showViewDialog2() {"));
		sList.add(new Statement(serial+3000L,3,"this.form = this.selectList2[0]"));
		sList.add(new Statement(serial+4000L,3,"this.viewDialogVisible2 = true"));
		sList.add(new Statement(serial+5000L,2,"},"));
		
		sList.add(new Statement(serial+9000L,1,"}"));
		sList.add(new Statement(serial+10000L,0,"}"));
		StatementList result =  WriteableUtil.merge(sList);
		result.setSerial(this.serial);
		return result;
	}

	@Override
	public boolean parse() throws Exception{
		if (this.diagram == null) {
			ListActiveCompareOption laco = new ListActiveCompareOption(this.planDomain,this.planxAxisFields,this.planyName,this.actionDomain,this.actionxAxisFields,this.actionyName);
			ListActiveCompareSumOption lacso = new ListActiveCompareSumOption(this.planDomain,this.planxAxisFields,this.planyName,this.actionDomain,this.actionxAxisFields,this.actionyName);
			this.diagram = new EchartsCompareDiagram(planDomain,planxAxisFields,planyName,actionDomain,actionxAxisFields,actionyName);
			
			this.planGrid = new DiagramDataControlGrid("");
			this.planGrid.setDomain(this.planDomain);
			
			this.actionGrid = new DiagramDataControlGrid("action");
			this.actionGrid.setDomain(this.actionDomain);
		
			this.viewPlanDialog = new ViewDialog(this.planDomain,"");
			
			this.viewActionDialog = new ViewDialog(this.actionDomain,"2");
			
			this.tmenubar = new ToggleMenubar(this.planDomain);
			this.tmenubar2= new ToggleMenubar(this.actionDomain);			
		}
		return true;		
	}

	public FrontDomain getPlanDomain() {
		return planDomain;
	}

	public void setPlanDomain(FrontDomain planDomain) {
		this.planDomain = planDomain;
	}

	public FrontDomain getActionDomain() {
		return actionDomain;
	}

	public void setActionDomain(FrontDomain actionDomain) {
		this.actionDomain = actionDomain;
	}

	public List<FrontField> getPlanxAxisFields() {
		return planxAxisFields;
	}

	public void setPlanxAxisFields(List<FrontField> planxAxisFields) {
		this.planxAxisFields = planxAxisFields;
	}

	public FrontField getPlanyName() {
		return planyName;
	}

	public void setPlanyName(FrontField planyName) {
		this.planyName = planyName;
	}

	public FrontField getActionyName() {
		return actionyName;
	}

	public void setActionyName(FrontField actionyName) {
		this.actionyName = actionyName;
	}

	public EchartsCompareDiagram getDiagram() {
		return diagram;
	}

	public void setDiagram(EchartsCompareDiagram diagram) {
		this.diagram = diagram;
	}

	public DiagramDataControlGrid getPlanGrid() {
		return planGrid;
	}

	public void setPlanGrid(DiagramDataControlGrid planGrid) {
		this.planGrid = planGrid;
	}

	public DiagramDataControlGrid getActionGrid() {
		return actionGrid;
	}

	public void setActionGrid(DiagramDataControlGrid actionGrid) {
		this.actionGrid = actionGrid;
	}

	public ViewDialog getViewPlanDialog() {
		return viewPlanDialog;
	}

	public void setViewPlanDialog(ViewDialog viewPlanDialog) {
		this.viewPlanDialog = viewPlanDialog;
	}

	public ViewDialog getViewActionDialog() {
		return viewActionDialog;
	}

	public void setViewActionDialog(ViewDialog viewActionDialog) {
		this.viewActionDialog = viewActionDialog;
	}

	public List<FrontField> getActionxAxisFields() {
		return actionxAxisFields;
	}

	public void setActionxAxisFields(List<FrontField> actionxAxisFields) {
		this.actionxAxisFields = actionxAxisFields;
	}

	@Override
	public StatementList generateStatements() throws Exception {
		List<Writeable> sList = new ArrayList<>();
		StatementList sl = this.generateLayoutStatements();
		StatementList sls = this.generateLayoutScriptStatements();
		sl.setSerial(2000L);
		sls.setSerial(5000L);
		
		sList.add(new Statement(1000L,0,"<template>"));
		sList.add(sl);	
		sList.add(new Statement(3000L,0,"</template>"));
		sList.add(new Statement(4000L,0,"<script>"));
		sList.add(sls);
		sList.add(new Statement(6000L,0,"</script>"));		
		return WriteableUtil.merge(sList);
	}

	public ToggleMenubar getTmenubar() {
		return tmenubar;
	}

	public void setTmenubar(ToggleMenubar tmenubar) {
		this.tmenubar = tmenubar;
	}

	public ToggleMenubar getTmenubar2() {
		return tmenubar2;
	}

	public void setTmenubar2(ToggleMenubar tmenubar2) {
		this.tmenubar2 = tmenubar2;
	}
	
}
